package com.example.test_01.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.test_01.R;
import com.example.test_01.model.User;
import com.example.test_01.service.UserService;
import com.example.test_01.service.UserServiceImpl;
import com.example.test_01.utils.Result;

import java.util.Date;

public class Login extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "LOGIN";
    EditText InputUsername;
    EditText InputPassword;
    Button BtnLogin;
    Button BtnForceLogin;
    UserService userService = new UserServiceImpl();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        InputUsername = findViewById(R.id.input_username);
        InputPassword = findViewById(R.id.input_password);
        BtnLogin = findViewById(R.id.btn_login);
        BtnLogin.setOnClickListener(this);
        BtnForceLogin = findViewById(R.id.btn_force_login);
        BtnForceLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_login:
                String Username = InputUsername.getText().toString();
                String Password = InputPassword.getText().toString();
                User user = new User();
                user.setUsername(Username);
                user.setPassword(Password);
                Result rec = userService.login(user);
                if(rec == Result.SUCCESS){
                    // 登录成功
                    Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(this,Home.class);
                    intent.putExtra("username",Username);
                    startActivity(intent);
                    finish();
                } else {
                    // 登录失败
                    Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_force_login:
                // 强制登录
                Intent intent=new Intent("com.example.logintestactivity.FORCE_OFFLINE");
                sendBroadcast(intent);
        }
    }
}